

//constant
var ok = "text-success glyphicon glyphicon-ok warnMsg";
var warn = "text-danger warnMsg";
//var warn = "alert alert-warning warnMsg";

$(document).ready(function(){
	
	/**
	 * 抓取书籍目录
	 */
	$("#catchBtn").click(function(){
		var webName = $("#webName").val();
		var catalogURL = $("#catalogURL").val();
//		alert(catalogURL +","+ webName);
		if(webName == null || webName.trim() == ""){
			$("#webNameWarning").html("<b>* 请选择来源...</b>");
		}else if(catalogURL == null || catalogURL.trim() == ""){
			$("#catalogWaring").html("<b>* 请输入URL...</b>").attr("class",warn);
		}else{
			$("#webName").attr("disabled","disabled");
			$("#catalogURL").attr("disabled","disabled");
			$("#catalogWaring").html("");
			$("#catchBtn").text("正在抓取...").attr("disabled","disabled");
			
			//catch success
			$("#catalogWaring").html("").attr("class",ok);
		}
	});
	
	
	/**
	 * 保存
	 */
	$("#saveBtn").click(function(){
		if(chechForm()){
			$(this).text("正在保存...");
			$("input").attr("disabled","disabled");
			$("select").attr("disabled","disabled");
			$("textarea").attr("disabled","disabled");
			$("button").attr("disabled","disabled");
		}
	});
	
	
	
	/**
	 * onfocus and onblur
	 */
	$("#name").blur(function(){
		checkName();
	});
	$("#author").blur(function(){
		checkAuthor();
	});
	$("#intro").blur(function(){
		checkIntro();
	});
	
});

/**
 * 图片预览
 * @param input
 */
function yulan(input) {
//    alert(input.size);
   if (input.files && input.files[0]) {
       var reader = new FileReader();
       reader.onload = function(e) {// 文件读取成功时的触发
            $("#previewImage").attr("src", e.target.result)
            				  .css("width","100px")
            				  .css("height" ,"120px" );
            var path = $(input).val();
//            if(path == null || path.trim() == ""){
//            	$("#imageWarning").html("<b>* 请选择图片...</b>").attr("class",warn);
//            }else{
            	$("#imageWarning").html("").attr("class",ok);
//            }
       };
       reader.readAsDataURL(input.files[0]);
   }
}

/**
 * check form
 */
function chechForm(){
	var elements = $(".warnMsg");
	var flag = true;
	for(var i=0;i<elements.size();i++){
		var element = elements.get(i);
		if($(element).attr("class") == warn){
			if($(element).attr("id") == "catalogWaring"){
				$(element).html("<b>* 请先抓取目录...</b>").attr("class",warn);
			}else{
				$(element).html("<b>* 不能为空...</b>").attr("class",warn);
			}
			
			flag =  false;
		}
	}
	if(flag == false){
		return false;
	}
	return true;
}

function checkName(){
	var name = $("#name").val();
	if(name == null || name.trim() == ""){
		$("#nameWarning").html("<b>* 请输入书名...</b>").attr("class",warn);
	}else{
		$("#nameWarning").html("").attr("class",ok);
	}
}
function checkAuthor(){
	var webName = $("#author").val();
	if(webName == null || webName.trim() == ""){
		$("#authorWarning").html("<b>* 请输入作者名...</b>").attr("class",warn);
	}else{
		$("#authorWarning").html("").attr("class",ok);
	}
}
function checkIntro(){
	var intro = $("#intro").val();
	if(intro == null || intro.trim() == ""){
		$("#introWarning").html("<b>* 请输入书籍简介...</b>").attr("class",warn);
	}else{
		$("#introWarning").html("").attr("class",ok);
	}
}



